<html>
<head><meta charset="utf-8"><title>SimplifyArmIdentity on matching layouts · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html">SimplifyArmIdentity on matching layouts</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205048114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205048114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Vandel Sillesen <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205048114">(Jul 26 2020 at 11:26)</a>:</h4>
<p>I am trying to resolve <a href="https://github.com/rust-lang/rust/blob/13f9aa190957b993a268fd4a046fce76ca8814ee/src/librustc_mir/transform/simplify_try.rs#L292">https://github.com/rust-lang/rust/blob/13f9aa190957b993a268fd4a046fce76ca8814ee/src/librustc_mir/transform/simplify_try.rs#L292</a> such that the optmization can run on code like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">enum</span> <span class="nc">MyResult</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span><span class="n">E</span><span class="o">&gt;</span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">T</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">E</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">id</span><span class="p">(</span><span class="n">r</span>: <span class="nb">Result</span><span class="o">&lt;</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">MyResult</span><span class="o">&lt;</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">MyResult</span>::<span class="nb">Ok</span><span class="p">(</span><span class="n">x</span><span class="p">),</span><span class="w"></span>
<span class="w">        </span><span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">MyResult</span>::<span class="nb">Err</span><span class="p">(</span><span class="n">y</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>My current progress is that I have changed the condition so that it checks if the layouts match. My problem arrives when trying to optimize the above test case.</p>
<div class="codehilite"><pre><span></span><code>encountered `Assign` statement with incompatible types:
left-hand side has type: MyResult&lt;u8, i32&gt;
right-hand side has type: std::result::Result&lt;u8, i32&gt;
  --&gt; /home/simon/Documents/rust/src/test/mir-opt/simplify-arm-same-layout.rs:11:18
   |
11 |         Ok(x) =&gt; MyResult::Ok(x),
   |                  ^^^^^^^^^^^^^^^
   |
   = note: delayed at src/librustc_mir/transform/validate.rs:140:36
</code></pre></div>


<p>The layouts match, but the types don't. How would I align the types so the validater won't complain? I see a Cast RValue, but is that only for primitives/variant casting? Else, a call to transmute could work. But then the inliner has to then inline that call, which seems like a roundabout way of doing it.<br>
Any other tricks?</p>



<a name="205048400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205048400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205048400">(Jul 26 2020 at 11:35)</a>:</h4>
<p>hmm... I've wanted type-ignorant assignments before... Maybe we should just have a flag on <code>StatementKind::Assign</code> that says whether the assignment is actually a transmute (we could even lower transmute calls to such assignments then!) cc <span class="user-mention" data-user-id="120791">@RalfJ</span> <span class="user-group-mention" data-user-group-id="1162">@WG-mir-opt</span></p>



<a name="205110744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205110744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205110744">(Jul 27 2020 at 12:32)</a>:</h4>
<p>I see this as a design decision we could have either way: do we want assignment to well-typed in MIR or not?</p>



<a name="205110814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205110814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205110814">(Jul 27 2020 at 12:33)</a>:</h4>
<p>these optimizations provide a motivation for at least also having an untyped ("transmuting") assignment operation, though I cannot judge if the optimization will be useful enough to be worth that trouble. if yes, we could either say that all assignments are "transmuting" or we add a flag.</p>



<a name="205110857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205110857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205110857">(Jul 27 2020 at 12:34)</a>:</h4>
<p>I am not entirely sure what benefit we would have from that flag though.</p>



<a name="205111753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205111753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205111753">(Jul 27 2020 at 12:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts/near/205110857">said</a>:</p>
<blockquote>
<p>I am not entirely sure what benefit we would have from that flag though.</p>
</blockquote>
<p>mainly MIR simplicity, as we get rid of <code>transmute</code> calls in terminators, but since we don't have first class support for other intrinsics... Not sure how much sense that makes. I think at the rust all hands <span class="user-mention" data-user-id="119009">@eddyb</span> and others talked about adding non-unwinding function calls as statements in MIR, maybe that would be a better approach for this, too</p>



<a name="205122762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205122762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205122762">(Jul 27 2020 at 14:22)</a>:</h4>
<p>extended basic blocks.</p>



<a name="205122805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205122805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205122805">(Jul 27 2020 at 14:22)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/issues/39685">https://github.com/rust-lang/rust/issues/39685</a></p>



<a name="205124829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205124829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205124829">(Jul 27 2020 at 14:38)</a>:</h4>
<p>thanks! Right, that was even more powerful</p>



<a name="205380280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380280">(Jul 29 2020 at 16:30)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> </p>
<blockquote>
<p>mainly MIR simplicity, as we get rid of transmute calls in terminators</p>
</blockquote>
<p>I meant, what does the flag gain us compared to just allowing transmute on all assignments</p>



<a name="205380357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380357">(Jul 29 2020 at 16:31)</a>:</h4>
<p>oh... no accidental introduction of transmuting assignments?</p>



<a name="205380518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380518">(Jul 29 2020 at 16:32)</a>:</h4>
<p>hm, maybe it helps as a safety net... not sure it's worth the effort though. but then, we already did all the work of implementing the check.^^</p>



<a name="205380708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380708">(Jul 29 2020 at 16:34)</a>:</h4>
<p>oh suddenly you think safety nets may not be worth the effort? <code>Scalar::Bits { size, ..}</code> <em>cough</em> <em>cough</em> <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="205380803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380803">(Jul 29 2020 at 16:35)</a>:</h4>
<p>joking aside, Until we have a good argument to remove it, I think we should just keep it if it doesn't have any perf or memory regressions</p>



<a name="205380828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380828">(Jul 29 2020 at 16:35)</a>:</h4>
<p>of course we need to be careful around <code>StatementKind</code> and increasing its size</p>



<a name="205380868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205380868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205380868">(Jul 29 2020 at 16:35)</a>:</h4>
<p>The no transmuting assignment assertion in cg_clif has saved me countless times.</p>



<a name="205568803"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205568803" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205568803">(Jul 31 2020 at 08:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts/near/205380708">said</a>:</p>
<blockquote>
<p>oh suddenly you think safety nets may not be worth the effort? <code>Scalar::Bits { size, ..}</code> <em>cough</em> <em>cough</em> <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>
</blockquote>
<p>fair, but that safety nat has proven to be effective at catching bugs. ;) the other one -- so far not, did it?</p>



<a name="205568855"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205568855" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205568855">(Jul 31 2020 at 08:28)</a>:</h4>
<p>but it looks like it was useful for <span class="user-mention" data-user-id="133247">@bjorn3</span> so that's a good argument</p>



<a name="205568924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205568924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205568924">(Jul 31 2020 at 08:29)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span> do you have your own check there and what does it look like? it turned out to be surprisingly non-trivial to properly check for this, see <a href="https://github.com/rust-lang/rust/blob/ac91673d895a0c578ed773e1280bdde8adb87b8c/src/librustc_mir/transform/validate.rs#L183">https://github.com/rust-lang/rust/blob/ac91673d895a0c578ed773e1280bdde8adb87b8c/src/librustc_mir/transform/validate.rs#L183</a></p>



<a name="205571906"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/205571906" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#205571906">(Jul 31 2020 at 09:13)</a>:</h4>
<p>This is the check I use: <a href="https://github.com/bjorn3/rustc_codegen_cranelift/blob/c5899526e4a6244ca200900e3f825c72e8ad5559/src/value_and_place.rs#L411-L462">https://github.com/bjorn3/rustc_codegen_cranelift/blob/c5899526e4a6244ca200900e3f825c72e8ad5559/src/value_and_place.rs#L411-L462</a></p>
<p>Basically it matches on the type. If it is a kind that has late bound regions then those are erased and it recurses. Otherwise it asserts that the types are equal. All early bound lifetimes are already erased by <code>fx.layout_of</code> before that function is reached.</p>



<a name="206040475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/206040475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#206040475">(Aug 05 2020 at 15:58)</a>:</h4>
<p><span class="user-mention" data-user-id="133247">@bjorn3</span>  okay so this should be functionally equivalent to <a href="https://github.com/rust-lang/rust/blob/ac91673d895a0c578ed773e1280bdde8adb87b8c/src/librustc_mir/transform/validate.rs#L41">https://github.com/rust-lang/rust/blob/ac91673d895a0c578ed773e1280bdde8adb87b8c/src/librustc_mir/transform/validate.rs#L41</a> ?</p>



<a name="206044760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity%20on%20matching%20layouts/near/206044760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/SimplifyArmIdentity.20on.20matching.20layouts.html#206044760">(Aug 05 2020 at 16:30)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> Yes, except that it also ignores mutability of references. At least in the past casting from <code>&amp;mut T</code> to <code>&amp;T</code> could happen during assignments. I don't know if this is still true though.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>